package com.mall.db.api;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.mapper.PmsProductAttributeCategoryMapper;
import com.mall.pojo.PmsBrand;
import com.mall.pojo.PmsProductAttributeCategory;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class PmsProductAttributeCategoryApiAmpl implements PmsProductAttributeCategoryApi {

    @Autowired
    private PmsProductAttributeCategoryMapper pmsProductAttributeCategoryMapper;

    //添加商品属性分类
    @Override
    public void create(String name) {
        PmsProductAttributeCategory ppac = new PmsProductAttributeCategory();
        ppac.setName(name);
        pmsProductAttributeCategoryMapper.insert(ppac);
    }

    //删除单个商品属性分类
    @Override
    public void delete(Integer id) {
        pmsProductAttributeCategoryMapper.deleteById(id);
    }

    //分页获取所有商品属性分类
    @Override
    public Page<PmsProductAttributeCategory> list(Integer pageNum, Integer pageSize) {
        //创建分页对象，设置分页参数
        //注意：使用分页，需要配置分页插件！！！
        Page<PmsProductAttributeCategory> pg = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<PmsProductAttributeCategory> wrapper = new LambdaQueryWrapper<>();
        return pmsProductAttributeCategoryMapper.selectPage(pg, wrapper);

    }

    //修改商品属性分类
    @Override
    public void update(Integer id, String name) {
        PmsProductAttributeCategory ppac = new PmsProductAttributeCategory();
        ppac.setId(Convert.toLong(id));
        ppac.setName(name);
        pmsProductAttributeCategoryMapper.updateById(ppac);
    }

    //获取单个商品属性分类信息
    @Override
    public PmsProductAttributeCategory selectById(Integer id) {
        return pmsProductAttributeCategoryMapper.selectById(id);
    }

    //获取所有商品属性分类
    @Override
    public List<PmsProductAttributeCategory> selectAll() {
        LambdaQueryWrapper<PmsProductAttributeCategory> wrapper = new LambdaQueryWrapper<>();
        return pmsProductAttributeCategoryMapper.selectList(wrapper);
    }
}
